#include "LCD.h"

#include <WProgram.h>
#include <Print.h>


LCD::LCD(Print *serial) {
  this->serial = serial;
}

void LCD::write(uint8_t b) {
  serial->write(b);
}

void LCD::setBrightness(int brightness) {
  if (brightness > LCD_BACKLIGHT_ON) brightness = LCD_BACKLIGHT_ON;
  if (brightness < LCD_BACKLIGHT_OFF) brightness = LCD_BACKLIGHT_OFF;
  sendConfigCommand(LCD_BACKLIGHT_BASE + brightness);
}

void LCD::setSplashScreen(const char *line1, const char *line2) {
  printLine1(line1);
  printLine2(line2);
  setSplashScreen();
}

void LCD::setSplashScreen() {
  sendConfigCommand(10);
}

void LCD::toggleSplashScreen() {
  sendConfigCommand(0x09);
}

void LCD::clearDisplay() {
  sendDisplayCommand(0x01);
}

void LCD::cursorRight(const int distance) {
  for (int i = 0; i < distance; i++) {
    sendDisplayCommand(0x14);
  }
}

void LCD::cursorLeft(const int distance) {
  for (int i = 0; i < distance; i++) {
    sendDisplayCommand(0x10);
  }
}

void LCD::scrollRight(const int distance) {
  for (int i = 0; i < distance; i++) {
    sendDisplayCommand(0x1C);
  }
}

void LCD::scrollLeft(const int distance) {
  for (int i = 0; i < distance; i++) {
    sendDisplayCommand(0x18);
  }
}

void LCD::visualDisplayOn() {
  sendDisplayCommand(0x0C);
}

void LCD::visualDisplayOff() {
  sendDisplayCommand(0x08);
}

void LCD::underlineCursorOn() {
  sendDisplayCommand(0x0E);
}

void LCD::underlineCursorOff() {
  sendDisplayCommand(0x08);
}

void LCD::blinkingCursonOn() {
  sendDisplayCommand(0x0D);
}

void LCD::blinkingCursorOff() {
  sendDisplayCommand(0x0C);
}

void LCD::setCursorPosition(uint8_t position) {
  sendDisplayCommand(0x80|position);
}
  
void LCD::setCursorLine1() {
  setCursorPosition(0);
}

void LCD::setCursorLine2() {
  setCursorPosition(64);
}

void LCD::printLine1(const char *line) {
  setCursorLine1();
  serial->print(line);
}

void LCD::printLine2(const char *line) {
  setCursorLine2();
  serial->print(line);
}

void LCD::sendConfigCommand() {
  serial->print(0x7C, BYTE);
}

void LCD::sendConfigCommand(uint8_t command) {
  serial->print(0x7C, BYTE);
  serial->print(command, BYTE);
}

void LCD::sendDisplayCommand() {
  serial->print(0xFE, BYTE);
}

void LCD::sendDisplayCommand(uint8_t command) {
  serial->print(0xFE, BYTE);
  serial->print(command, BYTE);
}

